package com.zd.lib.http.retrofit;


import com.google.gson.JsonParseException;
import com.google.gson.stream.MalformedJsonException;
import com.zd.lib.BuildConfig;
import com.zd.lib.http.exception.ResponseException;
import com.zd.lib.utils.LogUtils;
import com.zd.lib.utils.Prompt;

import org.json.JSONException;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.text.ParseException;

import rx.Subscriber;


/**
 * Created by zoudong on 16/7/20.
 * 用于统一 解析  CallBack
 */
public abstract class BaseSubscriber<T> extends Subscriber<T> {

    public static final  int NETWORKERROR =404;
    public static final  int SERVERERROR =500;
    public static final  int TIMEOUT=404;
    public static final  int FORCE_OUT = 1101; //封杀
    public static final  int OTHER_LOGOUT = 1100; //T出
    /**
     * Rx  回调 和 onError 互斥
     */
    @Override
    public void onCompleted() {

    }

    /**
     * Rx  回调 和 onCompleted 互斥
     */
    @Override
    public void onError(Throwable e) {
        if (isUnsubscribed()) {
            LogUtils.e("zoudong", "onError: 取消订阅");
            return;
        }
        e.printStackTrace();
        String message = "服务器出错，请稍后重试";//e.getLocalizedMessage() change by dsr 1
        int code=-1;
        if (e instanceof SocketTimeoutException||e instanceof UnknownHostException||e instanceof ConnectException) {
            message = "网络中断，请检查您的网络状态";
            code= NETWORKERROR;
            try {
                onNetWorkError();
            } catch (Exception e1) {
                //调用 onFailure 方法中抛出异常 将不会走 onError
            }

        } else if (e instanceof ResponseException){
            message = "服务器出错";
            code= SERVERERROR;
            Prompt.showToast(message);
            try {
                onFailure(code,message);
            } catch (Exception e1) {
                //调用 onFailure 方法中抛出异常 将不会走 onError
                LogUtils.e("zoudong", "onError: "+e1.toString());
            }
        } else if (e instanceof ResponseException) {  //code !=0  异常处理
            code= ((ResponseException)e).getCode();
            message = e.getMessage();
            try {
                onFailure(code,message);
            } catch (Exception e1) {
                //调用 onFailure 方法中抛出异常 将不会走 onError
                LogUtils.e("zoudong", "onError: "+e1.toString());
            }
        } else if (e instanceof JsonParseException
                || e instanceof JSONException
                || e instanceof ParseException
                || e instanceof MalformedJsonException) {  // 数据解析错误
            e.printStackTrace();
            message="解析异常";
            code=-990;
            if (BuildConfig.DEBUG) {
                Prompt.showToast(message);
            }
            try {
                onFailure(code,message);
            } catch (Exception e1) {
                //调用 onFailure 方法中抛出异常 将不会走 onError
                LogUtils.e("zoudong", "onError: "+e1.toString());
            }
        } else {//  其他 问题，空指针错误。。。。。 除了以上错误，其他错误都走这里
            try {
                onFailure(code,message);
            } catch (Exception e1) {
                //调用 onFailure 方法中抛出异常 将不会走 onError
                LogUtils.e("zoudong", "onError: "+e1.toString());
            }
            LogUtils.e("error", "<-------------------------------------------------------------");
            e.printStackTrace();
            LogUtils.e("error", "------------------------------------------------------------->");
        }

    }

    /**
     * 25代表游客登录，不弹toast
     * 3006-- 游客
     * @param code
     * @return
     */
    protected boolean isToast(int code) {
        return code != 25&&code!=3006;
    }

    /**
     * 处理错误消息  code!=0
     */
    public abstract void onFailure(int code, String msg);

    /**
     * 访问成功  code=0
     * @param t
     */
    public abstract void onSuccess(T t);

    /**
     * 网路错误
     */
    public void  onNetWorkError(){
        Prompt.showToast("网络中断，请检查您的网络状态");
    }

    /**
     * Rx  回调
     */
    @Override
    public void onNext(T t) {
        onSuccess(t);
    }

}
